package com.tsit.study.dubbo.consumer.controller;

import com.tsit.study.dubbo.consumer.go.UserProvider;
import com.tsit.study.dubbo.consumer.reference.UserReference;
import com.tsit.study.dubbo.provider.api.UserService;
import com.tsit.study.dubbo.provider.entity.User;
import com.tsit.study.dubbo.provider.feign.RemoteUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangxuzhi
 * @date 2020/11/17 17:37
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @DubboReference
    private UserService userService;

    @Autowired
    private RemoteUserService remoteUserService;

    @GetMapping("/info")
    public String getUserInfo() {
        return userService.getUserInfo();
    }

    @GetMapping("/list")
    public List<User> getUserList() {
        return userService.getUserList();
    }

    @GetMapping("/remote/list")
    public List<User> getRemoteUserList() {
        return remoteUserService.getUserList();
    }

    @GetMapping("/test/{count}")
    public Map<String, Long> test(@PathVariable("count") Integer count) {
        long s1 = System.currentTimeMillis();
        for(int i = 0; i < count; i ++) {
            remoteUserService.getUserList();
        }
        long e1 = System.currentTimeMillis();

        long s2 = System.currentTimeMillis();
        for(int i = 0; i < count; i ++){
            userService.getUserList();
        }
        long e2 = System.currentTimeMillis();

        Map<String, Long> result = new HashMap<>();
        result.put("dubbo", e2-s2);
        result.put("feign", e1-s1);
        return result;
    }
}
